Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@sinonjs/fake-timers

Package Overview
Dependencies
Maintainers
6
Versions
37
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@sinonjs/fake-timers

Fake JavaScript timers

  • 13.0.5
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
6
Created

What is @sinonjs/fake-timers?

The @sinonjs/fake-timers package is a utility for testing time-dependent code. It allows developers to simulate the passing of time in a controlled manner, without actually waiting. This is particularly useful for unit tests where you want to test timeouts, intervals, or the current date without causing delays in your test suite.

What are @sinonjs/fake-timers's main functionalities?

Simulating setTimeout

This feature allows you to simulate the behavior of setTimeout. By advancing the clock, you can immediately trigger timeouts without waiting.

const { install } = require('@sinonjs/fake-timers');
const clock = install();
setTimeout(() => console.log('Timeout called'), 100);
clock.tick(100); // Simulates the passing of 100 milliseconds

Simulating setInterval

This feature enables the simulation of setInterval. You can advance the clock to trigger intervals as many times as needed without real time passing.

const { install } = require('@sinonjs/fake-timers');
const clock = install();
setInterval(() => console.log('Interval called'), 100);
clock.tick(300); // Simulates the passing of 300 milliseconds, triggering the interval 3 times

Simulating Date

This feature allows you to set the current date to a specific timestamp, enabling you to test code that depends on the current date without relying on the system's clock.

const { install } = require('@sinonjs/fake-timers');
const clock = install({ now: 1577836800000 });
console.log(new Date()); // Logs the fake current date set to the start of 2020

Other packages similar to @sinonjs/fake-timers

FAQs

Package last updated on 29 Oct 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc